package leetcode.二维数组;

public class Leetcode304 {
    class NumMatrix {
        int[][] help;
        public NumMatrix(int[][] matrix) {
            if(matrix==null||matrix[0]==null){
                return;
            }
            int rows = matrix.length;
            int cols = matrix[0].length;
            help = new int[rows+1][cols+1];
            for (int i = 1; i <= rows; i++) {
                for (int j = 1; j <= cols; j++) {

                    help[i][j] = help[i-1][j]+help[i][j-1]+matrix[i-1][j-1]-help[i-1][j-1];
                }
            }
        }

        public int sumRegion(int row1, int col1, int row2, int col2) {
            int tmp = help[row1][col2+1] + help[row2+1][col1] - help[row1][col1];
            return help[row2+1][col2+1] - tmp;
        }
    }
}
